<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class J{
	static private $instance=NULL;
	public $config;
	public $session;
	public $db;
	public $view;

	/*
	 * 设置$this->config 得到 config文件夹下的config.php的配置
	 */
	public function __construct() {
		
	}

	//为了让ide不报警告
	public function init(){}

	/*
	 * $php=J::getInstance();
	 * 单例模式创造启动变量$php
	 */
	static function getInstance()
	{
	    if(self::$instance == NULL )
	    {
	        self::$instance = new J();
	    }
	    return self::$instance;
	}

	public function helper($filename)
	{
	    include_once $filename.'_helper.php';
	    return $this;
	}
    
	public function loaddb() {
		$conf = C("db");
		include "dbtest.class.php";
		$this->db = new dbtest($conf);
		return $this;
	}
	
	public function loadview() {
		include "viewtest.class.php";
		$this->view = new viewtest(TPL);
		return $this;
		/*
			暂时还没确定用哪个模板引擎
		*/
	}

    public function session()
    {
        //require_once 'Zend/Session.php';
        //Zend_Session::start();
        include_once 'Zend/Session/Namespace.php';
        $this->session = new Zend_Session_Namespace('cake');//session命名空间统一为 cake ^0^
        return $this;
    }
	
	function M($name) {
	
		include_once 'm.php';
	
		$name = "m_".$name;
	
		include MODULE."m/".$name.".php";
		
		$m = new $name($this);
		
		return $m;
	
	}

    // 跳转
    function redirect($url,$time=0,$message="")
    {
        $time = (int)$time;
        if ($time > 0) {
echo <<<EOT
        <html>
        <head>
        <meta content="text/html; charset=UTF-8" http-equiv="content-type"/>
        <meta http-equiv="refresh" content="{$time};URL={$url}" />
        </head>
        <body>
        {$message}
        </body>
        </html>
EOT;
        } else {
            header("Location: {$url}");
        }
        exit;
    }
    //------------------------

}





function __autoload($classname) {
	$myclass=explode("_",$classname);



	$reclass=$myclass[0];
	for($i=1;$i<=count($myclass)-1;$i++){
		$reclass=$reclass."/".$myclass[$i];
	}
	require_once ($reclass.".php");
	return;

}